package com.conversationboard.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * Send email using an authenticated, SSL-enabled SMTP server (e.g. GMail).
 * 
 * @author Keith Watson
 *
 */

class SslSmtpMail implements IMail {

	private String loginId;
	private String password;
	private String host;
	private int port;
	private Session session;

	private static class SMTPAuthenticator extends javax.mail.Authenticator {

		private String loginId;
		private String password;


		public SMTPAuthenticator(String email, String password) {
			this.loginId = email;
			this.password = password;
		}


		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(this.loginId, this.password);
		}

	}


	public SslSmtpMail(String loginId, String password, String host, int port) {

		this.setLoginId(loginId);
		this.setPassword(password);
		this.setHost(host);
		this.setPort(port);

		Properties properties = new Properties();

		properties.put("mail.smtp.user", this.getLoginId());
		properties.put("mail.smtp.host", this.getHost());
		properties.put("mail.smtp.port", this.getPort());
		properties.put("mail.smtp.starttls.enable", "true");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.socketFactory.port", (port + ""));
		properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.socketFactory.fallback", "false");

		Authenticator authenticator = new SMTPAuthenticator(this.getLoginId(), this.getPassword());
		Session session = Session.getInstance(properties, authenticator);
		this.setSession(session);

	}


	public void send(String from, String recipients, String subject, String body) throws MessagingException {

		MimeMessage message = new MimeMessage(this.getSession());

		message.setSubject(subject);
		message.setFrom(new InternetAddress(from));
		message.setSender(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));

		if (!body.trim().startsWith("<html")) {
			message.setText(body);
		} else {
			Multipart multiPart = new MimeMultipart();

			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setText(body);
			bodyPart.setHeader("content-type", "text/html");

			multiPart.addBodyPart(bodyPart);

			message.setContent(multiPart);
		}

		Transport.send(message);

	}


	public String getLoginId() {
		return loginId;
	}


	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getHost() {
		return host;
	}


	public void setHost(String host) {
		this.host = host;
	}


	public int getPort() {
		return port;
	}


	public void setPort(int port) {
		this.port = port;
	}


	public Session getSession() {
		return session;
	}


	public void setSession(Session session) {
		this.session = session;
	}

}